package com.example.shop.api;

import com.example.shop.base.ApiBaseController;
import com.example.shop.base.Result;
import com.example.shop.commodity.entity.ShopCart;
import com.example.shop.commodity.entity.ShopCategorys;
import com.example.shop.commodity.entity.ShopCommodity;
import com.example.shop.commodity.service.ShopCartService;
import com.example.shop.commodity.service.ShopCategorysService;
import com.example.shop.commodity.service.ShopCommodityService;
import com.example.shop.common.utils.Constants;
import com.example.shop.common.utils.RedisService;
import com.example.shop.domain.wx.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/shop/cart")
public class ApiShopCartController extends ApiBaseController {

    @Autowired
    private ShopCartService cartService;
    @Autowired
    private ShopCommodityService commodityService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private ShopCategorysService categorysService;

    @GetMapping("/list")
    public Result list(ShopCart shopCart) {
        UserInfo userInfo = (UserInfo) redisService.get(Constants.WX_API_TOKEN + getToken());
        shopCart.setUid(userInfo.getUid());
        shopCart.setStatus("0");
        List<ShopCart> list = cartService.findList(shopCart);
        list.forEach(i -> {
            ShopCommodity commodity = commodityService.get(i.getCid());
            if (null != commodity) {
                commodity.setPictures(picture_url + commodity.getPictures());
                i.setCommodity(commodity);
                ShopCategorys category = categorysService.get(commodity.getCid());
                if (null != category) {
                    i.setName(category.getName());
                }
            }
        });
        return Result.success(list);
    }

    @GetMapping("/total")
    public Result total() {
        UserInfo userInfo = (UserInfo) redisService.get(Constants.WX_API_TOKEN + getToken());
        int t = 0;
        if (null != userInfo) {
            List<ShopCart> list = cartService.selectTotal(userInfo.getUid());
            t = list.size();
        }
        return Result.success(t);
    }

    @PostMapping("/add")
    public Result addCart(ShopCart cart) {
        UserInfo userInfo = (UserInfo) redisService.get(Constants.WX_API_TOKEN + getToken());
        ShopCart sc = cartService.get(cart.getCid(), userInfo.getUid());
        if (sc != null) {
            sc.setNumber(sc.getNumber() + 1);
            cartService.save(sc);
        } else {
            cart.setUid(userInfo.getUid());
            cart.setNumber(1);
            cart.setStatus("0");
            cartService.save(cart);
        }

        return Result.success();
    }

    @PostMapping("/sub")
    public Result sub(ShopCart cart) {
        ShopCart sc = cartService.get(cart.getCid());
        if (sc != null && sc.getNumber() > 1) {
            sc.setNumber(sc.getNumber() - 1);
            cartService.save(sc);
        }
        return Result.success();
    }

    @PostMapping("/del")
    public Result del(String id) {
        cartService.logicalDeletion(id);
        return Result.success();
    }
}
